package org.pgis.gui;

import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.event.ActionListener;

import org.pgis.core.AbstractScenePanel;

public class DicomSettingsPanel extends JPanel{

	private static final long serialVersionUID = 1L;
	private AbstractScenePanel asp;
	private JTextField textSliceGap;
	private JTextField textSliceThickness;
	private JTextField textXPixelSpacing;
	private JTextField textYPixelSpacing;

	public DicomSettingsPanel(AbstractScenePanel asp) {
		this.asp = asp;
		initialize();
	}

	private void initialize() {

		this.setLayout(null);

		JLabel labelTitle = new JLabel("DICOM Settings");
		labelTitle.setFont(new Font("Century", Font.BOLD, 16));
		labelTitle.setBounds(62, 11, 150, 30);
		this.add(labelTitle);

		JLabel labelStart = new JLabel("Slice Gap");
		labelStart.setBounds(21, 52, 64, 27);
		add(labelStart);

		JLabel labelEnd = new JLabel("Slice Thickness");
		labelEnd.setBounds(21, 95, 106, 29);
		add(labelEnd);

		JLabel labelDistance = new JLabel("X - Pixel Spacing");
		labelDistance.setBounds(21, 140, 95, 24);
		add(labelDistance);

		JLabel labelEndValue = new JLabel("");
		labelEndValue.setBounds(95, 135, 137, 14);
		add(labelEndValue);

		textSliceGap = new JTextField();
		textSliceGap.setToolTipText("0018,0088");
		textSliceGap.setText("1.0");
		textSliceGap.setBounds(137, 52, 95, 30);
		add(textSliceGap);
		textSliceGap.setColumns(10);

		textSliceThickness = new JTextField();
		textSliceThickness.setToolTipText("0018,0050");
		textSliceThickness.setText("1.0");
		textSliceThickness.setBounds(137, 94, 95, 30);
		add(textSliceThickness);
		textSliceThickness.setColumns(10);

		textXPixelSpacing = new JTextField();
		textXPixelSpacing.setToolTipText("0028, 0030");
		textXPixelSpacing.setText("1.0");
		textXPixelSpacing.setBounds(137, 137, 95, 30);
		add(textXPixelSpacing);
		textXPixelSpacing.setColumns(10);

		JButton buttonApply = new JButton("Apply");
		buttonApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Float sliceGap = null, sliceTickness = null,
						xPixelSpacing = null, yPixelSpacing = null;

				try {
					if(!textSliceGap.getText().isEmpty()){
						sliceGap = Float.valueOf(textSliceGap.getText());
					}

					if(!textSliceThickness.getText().isEmpty()){
						sliceTickness = Float.valueOf(textSliceThickness.getText());
					}

					if(!textXPixelSpacing.getText().isEmpty()){
						xPixelSpacing = Float.valueOf(textXPixelSpacing.getText());
					}

					if(!textYPixelSpacing.getText().isEmpty()){
						yPixelSpacing = Float.valueOf(textYPixelSpacing.getText());
					}

				}catch(NumberFormatException ex) {
					ex.printStackTrace();
				}

				asp.getSim().setDicomProperties(sliceGap, sliceTickness, xPixelSpacing, yPixelSpacing);
				asp.refresh();
			}
		});
		buttonApply.setBounds(137, 220, 95, 23);
		add(buttonApply);

		JLabel lblNewLabel = new JLabel("Y - Pixel Spacing");
		lblNewLabel.setBounds(21, 187, 95, 24);
		add(lblNewLabel);

		textYPixelSpacing = new JTextField();
		textYPixelSpacing.setToolTipText("0028, 0030");
		textYPixelSpacing.setText("1.0");
		textYPixelSpacing.setBounds(137, 182, 95, 27);
		add(textYPixelSpacing);
		textYPixelSpacing.setColumns(10);

		JLabel lblMm = new JLabel("mm");
		lblMm.setBounds(242, 145, 46, 14);
		add(lblMm);

		JLabel lblMm_1 = new JLabel("mm");
		lblMm_1.setBounds(242, 102, 46, 14);
		add(lblMm_1);

		JLabel lblMm_2 = new JLabel("mm");
		lblMm_2.setBounds(242, 187, 46, 19);
		add(lblMm_2);

		JLabel lblMm_3 = new JLabel("mm");
		lblMm_3.setBounds(242, 58, 46, 14);
		add(lblMm_3);
	}
}
